package com.cuitrip.app.base;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.ViewGroup;

import com.cuitrip.service.R;
import com.lab.adapter.IAdapterData;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.component.list.CTRecyclerView;
import com.lab.component.list.event.OnMoreListener;
import com.lab.component.list.simple.CTSimpleRecyclerAdapter;
import com.lab.component.list.simple.CTSimpleViewHolder;
import com.lab.component.list.simple.SimpleAdapterInterface;
import com.lab.utils.LogHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * vertical list activity
 * Created by baziii on 15/8/10.
 */
public abstract class BaseVerticalListActivity<T extends IAdapterData> extends CustomUiFragmentActivity implements ILoadMoreListView<T>, SimpleAdapterInterface<T>, OnMoreListener {
    public static final String TAG = "BaseVerticalListActivity";
    @Bind(R.id.ct_recycler_view)
    protected CTRecyclerView mRecyclerView;
    protected SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            showRefreshView();
            requestPresentRefresh();
        }
    };
    LinearLayoutManager mLayoutManager;
    //包括刷新和loadMore
    boolean hasMore = true;
    private List<T> mDataList = new ArrayList<>();
    private CTSimpleRecyclerAdapter<T> mAdapter;
    private boolean isResumeRefresh = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.simple_refresh_vertical);
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);

        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new CTSimpleRecyclerAdapter<T>(this, mDataList) {
            @Override
            public int getItemLayoutId(int viewType) {
                return BaseVerticalListActivity.this.getItemLayoutId(viewType);
            }

            @Override
            public void onNewViewHolder(CTSimpleViewHolder viewHolder, ViewGroup parent, int viewType) {
                BaseVerticalListActivity.this.onNewViewHolder(viewHolder, parent, viewType);
            }

            @Override
            public void bindData(CTSimpleViewHolder holder, int position, T item) {
                BaseVerticalListActivity.this.bindData(holder, position, item);
            }
        };

        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setRefreshListener(onRefreshListener);
    }


    abstract public int getItemLayoutId(int viewType);

    abstract public void bindData(CTSimpleViewHolder holder, int position, T item);

    abstract public void onNewViewHolder(CTSimpleViewHolder viewHolder, ViewGroup parent, int viewType);

    @Override
    protected void onResume() {
        super.onResume();
        LogHelper.e(TAG, " is ui show refresh:" + mRecyclerView.getSwipeToRefresh().isRefreshing());
        if (mRecyclerView.getSwipeToRefresh().isRefreshing()) {
            mRecyclerView.setRefreshing(false);
            isResumeRefresh = true;
            mRecyclerView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (isResumeRefresh) {
                        mRecyclerView.setRefreshing(true);
                    }
                }
            }, 300);
        }
    }

    public void setHasMore(boolean hasMore) {
        this.hasMore = hasMore;
    }

    public abstract void requestPresentLoadMore();

    public abstract void requestPresentRefresh();

    public void showRefreshView() {
        LogHelper.e(TAG, "showRefreshView");
        mRecyclerView.setRefreshing(true);
    }

    public void hideRefreshView() {
        LogHelper.e(TAG, "hideRefreshView");
        if (mRecyclerView != null) {
            mRecyclerView.setRefreshing(false);
            isResumeRefresh = false;
        }
    }

    @Override
    public void showProgressView() {
        mRecyclerView.showProgress();
    }

    @Override
    public void hideProgressView() {
        mRecyclerView.showRecycler();
    }

    public void setupMoreLeftCount(int max) {
        mRecyclerView.setupMoreListener(this, max);
    }

    @Override
    public void showLoadingMoreView() {
        mRecyclerView.showMoreProgress();
    }

    @Override
    public void hideLoadingMoreView() {
        mRecyclerView.hideMoreProgress();
    }

    protected CTSimpleRecyclerAdapter<T> getAdapter() {
        return mAdapter;
    }

    @Override
    public void onMoreAsked(int overallItemsCount, int itemsBeforeMore, int maxLastVisiblePosition) {
        requestPresentLoadMore();
    }

}
